package main.java.com.bookmanager.util;

import main.java.com.bookmanager.model.Book;

import java.util.*;

public class MemoryStorage {
    private static Map<Classify, List<Book>> books;

    public MemoryStorage() {
        books = new HashMap<>();
        for (Classify classify : Classify.values()) {
            books.put(classify, new ArrayList<>());
        }
    }

    public Map<Classify, List<Book>> getBooks() {
        return books;
    }

    public void setBooks(Map<Classify, List<Book>> books) {
        this.books = books;
    }

    //添加图书
    public boolean addBooks(Classify classify, Book book) {
        if (classify == null) {
            return false;
        }
        List<Book> categoryBooks = books.computeIfAbsent(classify, k -> new ArrayList<>());
        return categoryBooks.add(book);
    }

    //删除图书（ID）
    public boolean deleteBooksId(Classify classify, Integer id) {
        if (classify == null) {
            return false;
        }
        List<Book> categoryBooks = books.get(classify);
        if (categoryBooks != null) {
            for (int i = categoryBooks.size() - 1; i >= 0; i--) {
                if (id.equals(categoryBooks.get(i).getBookId())) {
                    return categoryBooks.remove(categoryBooks.get(i));
                }
            }
        }
        return false;
    }

    //删除图书（名字）
    public boolean deleteBooksName(Classify classify, String name) {
        if (classify == null) {
            return false;
        }
        List<Book> categoryBooks = books.get(classify);
        if (categoryBooks != null) {
            for (int i = categoryBooks.size() - 1; i >= 0; i--) {
                if (name.equals(categoryBooks.get(i).getBookName())) {
                    return categoryBooks.remove(categoryBooks.get(i));
                }
            }
        }
        return false;
    }

    //查找图书(名字)
    public Book getBooksName(Classify classify, String name) {
        if (classify == null || name == null) {
            return null;
        }
        List<Book> categoryBooks = books.get(classify);
        if (categoryBooks != null) {
            for (int i = 0; i < categoryBooks.size(); i++) {
                if (name.equals(categoryBooks.get(i).getBookName())) {
                    return categoryBooks.get(i);
                }
            }
        }
        return null;
    }

    //查找图书(ID)
    public Book getBooksId(Classify classify, Integer id) {
        if (classify == null) {
            return null;
        }
        List<Book> categoryBooks = books.get(classify);
        if (categoryBooks != null) {
            for (int i = 0; i < categoryBooks.size(); i++) {
                if (id.equals(categoryBooks.get(i).getBookId())) {
                    return categoryBooks.get(i);
                }
            }
        }
        return null;
    }

    //查找图书(作者)
    public List<Book> getBooksAuthor(Classify classify, String author) {
        if (classify == null) {
            return null;
        }
        List<Book> bookList = new ArrayList<>();
        List<Book> categoryBooks = books.get(classify);
        if (categoryBooks != null) {
            for (int i = 0; i < categoryBooks.size(); i++) {
                if (author.equals(categoryBooks.get(i).getBookAuthor())) {
                    bookList.add(categoryBooks.get(i));
                }
            }
        }
        return bookList;
    }

    //更改图书信息(名字)
    public boolean amendBooksName(Classify classify, String name, int id, String bookName, String bookAuthor, String status, Classify newClassify, Date createTime,int quantity) {
        if (classify == null) {
            return false;
        }
        List<Book> categoryBooks = books.get(classify);
        if (categoryBooks != null) {
            for (int i = 0; i < categoryBooks.size(); i++) {
                if (name.equals(categoryBooks.get(i).getBookName())) {
                    categoryBooks.remove(categoryBooks.get(i));
                    Book newBook = new Book(id, bookName, bookAuthor, newClassify, status, createTime, quantity);
                    return books.get(newClassify).add(newBook);
                }
            }
        }
        return false;
    }

    //更改图书名字（id）
    public boolean amendBooksId(Classify classify, int amendId, int id, String bookName, String bookAuthor, Classify newClassify, String status, Date createTime, int quantity) {
        if (classify == null) {
            return false;
        }
        List<Book> categoryBooks = books.get(classify);
        if (categoryBooks != null) {
            for (int i = 0; i < categoryBooks.size(); i++) {
                if (amendId == categoryBooks.get(i).getBookId()) {
                    categoryBooks.remove(categoryBooks.get(i));
                    Book newBook = new Book(id, bookName, bookAuthor, newClassify, status, createTime, quantity);
                    return books.get(newClassify).add(newBook);
                }
            }
        }
        return false;
    }

    //打印当前分类所有图书
    public void printBooks(Classify classify) {
        if (classify == null) {
            return;
        }
        System.out.println("ID\t名字\t作者\t种类\t目前状态\t创建时间");
        List<Book> categoryBooks = books.get(classify);
        if (categoryBooks != null) {
            for (Book book : categoryBooks) {
                System.out.println(book);
            }
        }
    }

    //获取所有图书
    public List<Book> getAllBooks(Classify classify) {
        List<Book> categoryBooks = books.get(classify);
        return categoryBooks != null ? categoryBooks : Collections.emptyList();
    }

    //借出方法
    public boolean borrow(Book book){
        if (book == null) {
            return false;
        }
        if (book.getQuantity()>0){
            book.setQuantity(book.getQuantity()-1);
            book.setLend(book.getLend()+1);
            return true;
        }
        return false;
    }

    //还书方法
    public boolean returnBook(Book book){
        if (book == null) {
            return false;
        }
        if (book.getLend()>0){
            book.setLend(book.getLend()-1);
            book.setQuantity(book.getQuantity()+1);
            return true;
        }
        return false;
    }

}
